package com.harveyh.cemax;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.database.Cursor;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.harveyh.cemax.bean.LastReadInfoBean;
import com.harveyh.cemax.bean.ShiTiBean;
import com.harveyh.cemax.common.EActivity;
import com.harveyh.cemax.common.GloableData;
import com.harveyh.cemax.manager.DBManager;
import com.harveyh.cemax.manager.UtilsManager;

public class DatiActivity extends EActivity {
	private LinearLayout ll_answerA,ll_answerB,ll_answerC,ll_answerD,ll_timu;
	LinearLayout ll_alertContent,ll_answerDesc;
	EditText editText;
	private TextView tv_pre,tv_next,tv_remove2Cuoti,tv_showAnswer,tv_type,tv_timu,
					tv_optionA,tv_optionB,tv_optionC,tv_optionD,tv_timuCount,tv_answerDesc;
	private ImageView imgv_home,imgv_pic,imgv_optionA,imgv_optionB,imgv_optionC,imgv_optionD;
	private ScrollView scrollView;
	private LinearLayout ll_bottom;
	private boolean isCuoti;
	private int whichTable;
	private String timuType = "";
	private int receiveLocalId;
	boolean isAnswerVisible;
	/** 题目总数*/
	int timuCount;
	/** 当前题数*/
	int curIndex;
	/** 分页查询，保存当前正在查询的页数*/
	int page = 1;
	/** 每页取的条数*/
	int onePageSize = 20;
	int typeId = 0;
	String curTableName = "";
	ArrayList<ShiTiBean> list = new ArrayList<ShiTiBean>();
	ArrayList<Integer> timuIdsList = new ArrayList<Integer>();
	ShiTiBean curBean;// 当前显示的试题bean
	String rightAnswer = "";
	DBManager db;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dati_activity);
		if (savedInstanceState!=null) {
			UtilsManager.println("从状态保存里获取数据");
			receiveLocalId = savedInstanceState.getInt("receiveLocalId");
			whichTable = savedInstanceState.getInt("whichTable");
			isCuoti = savedInstanceState.getBoolean("isCuoti");
			timuType = savedInstanceState.getString("timuType");
		} else {
			receiveLocalId = getIntent().getIntExtra("localId", -1);
			whichTable = getIntent().getIntExtra("whichTable", 0);
			isCuoti = getIntent().getBooleanExtra("isCuoti", false);
			timuType = getIntent().getStringExtra("type");
		}
		if (TextUtils.isEmpty(timuType)) {
			timuType = "xuanze";
		}
		db = DBManager.open(getApplicationContext());
		init();
		switch (whichTable) {
		case 0:
			curTableName = GloableData.chujiTableName;
			break;
		case 1:
			curTableName = GloableData.zhongjiTableName;
			break;
		case 2:
			curTableName = GloableData.gaojiTableName;
			break;
		case 3:
			curTableName = GloableData.jigongTableName;
			break;
		case 4:
			curTableName = GloableData.gaojijishiTableName;
			break;
		case 5:
			curTableName = GloableData.chujiTableName;
			break;
		}
		timuCount = getTimuCountFromDB(curTableName,timuType,isCuoti);
		if (timuCount>0) {
			if (receiveLocalId<0){
				getTimuFromDB(getApplicationContext(),curTableName,timuType,isCuoti,page);
			} else {
				getTimuFromDBByLastRead(getApplicationContext(),curTableName,timuType,isCuoti,receiveLocalId);
			}
		} else {// 查不到数据
			scrollView.setVisibility(View.GONE);
			ll_bottom.setVisibility(View.GONE);
			Toast.makeText(this, "很抱歉，没有找到试题...", Toast.LENGTH_LONG).show();
		}
	}
	
	private void init() {
		scrollView = (ScrollView)findViewById(R.id.scrollView);
		ll_answerDesc = (LinearLayout)findViewById(R.id.linearLayout_answerDesc);
		ll_bottom = (LinearLayout)findViewById(R.id.linearLayout_bottom);
		ll_timu = (LinearLayout)findViewById(R.id.linearLayout_timu);
		ll_answerA = (LinearLayout)findViewById(R.id.linearLayout_anwserA);
		ll_answerB = (LinearLayout)findViewById(R.id.linearLayout_anwserB);
		ll_answerC = (LinearLayout)findViewById(R.id.linearLayout_anwserC);
		ll_answerD = (LinearLayout)findViewById(R.id.linearLayout_anwserD);
		tv_timu = (TextView)findViewById(R.id.tv_timu);
		tv_timuCount = (TextView)findViewById(R.id.tv_count);
		tv_optionA = (TextView)findViewById(R.id.tv_optionA);
		tv_optionB = (TextView)findViewById(R.id.tv_optionB);
		tv_optionC = (TextView)findViewById(R.id.tv_optionC);
		tv_optionD = (TextView)findViewById(R.id.tv_optionD);
		tv_pre = (TextView)findViewById(R.id.tv_pre);
		tv_next = (TextView)findViewById(R.id.tv_next);
		tv_remove2Cuoti = (TextView)findViewById(R.id.tv_remove2cuoti);
		tv_showAnswer = (TextView)findViewById(R.id.tv_showAnswer);
		tv_type = (TextView)findViewById(R.id.tv_type);
		tv_answerDesc = (TextView)findViewById(R.id.tv_answerDesc);
		imgv_pic = (ImageView)findViewById(R.id.imageView_pic);
		imgv_optionA = (ImageView)findViewById(R.id.imageView_optionA);
		imgv_optionB = (ImageView)findViewById(R.id.imageView_optionB);
		imgv_optionC = (ImageView)findViewById(R.id.imageView_optionC);
		imgv_optionD = (ImageView)findViewById(R.id.imageView_optionD);
		if (isCuoti) {
			tv_type.setText("错题练习");
		} else {
			tv_type.setText("顺序练习");
		}
		imgv_home = (ImageView)findViewById(R.id.imageView_home);
		
		ll_answerA.setOnClickListener(this.onClick());
		ll_answerB.setOnClickListener(this.onClick());
		ll_answerC.setOnClickListener(this.onClick());
		ll_answerD.setOnClickListener(this.onClick());
		tv_pre.setOnClickListener(this.onClick());
		tv_next.setOnClickListener(this.onClick());
		tv_remove2Cuoti.setOnClickListener(this.onClick());
		tv_showAnswer.setOnClickListener(this.onClick());
		imgv_home.setOnClickListener(this.onClick());
		tv_timuCount.setOnClickListener(this.onClick());
	}
	
	
	private void resetViewData(boolean isShowAnswer,boolean isOptionEnable,
			boolean isShowRight,boolean isShowRightErr) {
		if (curIndex>=list.size()){
			return;
		}
//		if (receiveLocalId==-1){
//			tv_timuCount.setText((curIndex+1)+"/"+timuCount);
//		} else {
//			tv_timuCount.setText((curIndex)+"/"+timuCount);
//		}
		tv_timuCount.setText((curIndex+1)+"/"+timuCount);
		curBean = list.get(curIndex);
		if (curBean.isCuoti == 0) {// 不是错题
			ll_timu.setBackgroundColor(Color.TRANSPARENT);
			tv_remove2Cuoti.setText("标记错题");
		} else {
			ll_timu.setBackgroundColor(Color.RED);
			tv_remove2Cuoti.setText("移除错题");
		}
		
		if (isShowAnswer) {
			tv_showAnswer.setText("答题模式");
			if ("panduan".equals(curBean.timuType)) {
				ll_answerDesc.setVisibility(View.VISIBLE);
				tv_answerDesc.setText(curBean.answerDesc);
			} else {
				ll_answerDesc.setVisibility(View.GONE);
			}
			if ("A".equals(curBean.answerOption)) {
				imgv_optionA.setImageResource(R.drawable.radio_icon2);
				imgv_optionB.setImageResource(R.drawable.radio_icon1);
				imgv_optionC.setImageResource(R.drawable.radio_icon1);
				imgv_optionD.setImageResource(R.drawable.radio_icon1);
			} else if ("B".equals(curBean.answerOption)) {
				imgv_optionA.setImageResource(R.drawable.radio_icon1);
				imgv_optionB.setImageResource(R.drawable.radio_icon2);
				imgv_optionC.setImageResource(R.drawable.radio_icon1);
				imgv_optionD.setImageResource(R.drawable.radio_icon1);
			} else if ("C".equals(curBean.answerOption)) {
				imgv_optionA.setImageResource(R.drawable.radio_icon1);
				imgv_optionB.setImageResource(R.drawable.radio_icon1);
				imgv_optionC.setImageResource(R.drawable.radio_icon2);
				imgv_optionD.setImageResource(R.drawable.radio_icon1);
			} else if ("D".equals(curBean.answerOption)) {
				imgv_optionA.setImageResource(R.drawable.radio_icon1);
				imgv_optionB.setImageResource(R.drawable.radio_icon1);
				imgv_optionC.setImageResource(R.drawable.radio_icon1);
				imgv_optionD.setImageResource(R.drawable.radio_icon2);
			}
		} else if (isShowRightErr) {
			tv_showAnswer.setText("查看答案");
			ll_answerDesc.setVisibility(View.GONE);
		} else if (isShowRight) {
			tv_showAnswer.setText("答题模式");
			if ("panduan".equals(curBean.timuType)) {
				ll_answerDesc.setVisibility(View.VISIBLE);
				tv_answerDesc.setText(curBean.answerDesc);
			} else {
				ll_answerDesc.setVisibility(View.GONE);
			}
			if ("A".equals(rightAnswer)) {
				imgv_optionA.setImageResource(R.drawable.radio_right);
			}else if ("B".equals(rightAnswer)) {
				imgv_optionB.setImageResource(R.drawable.radio_right);
			}else if ("C".equals(rightAnswer)) {
				imgv_optionC.setImageResource(R.drawable.radio_right);
			}else if ("D".equals(rightAnswer)) {
				imgv_optionD.setImageResource(R.drawable.radio_right);
			}
		} else {
			tv_showAnswer.setText("查看答案");
			ll_answerDesc.setVisibility(View.GONE);
			imgv_optionA.setImageResource(R.drawable.radio_icon1);
			imgv_optionB.setImageResource(R.drawable.radio_icon1);
			imgv_optionC.setImageResource(R.drawable.radio_icon1);
			imgv_optionD.setImageResource(R.drawable.radio_icon1);
		}
		if (isOptionEnable) {
			ll_answerA.setEnabled(true);
			ll_answerB.setEnabled(true);
			ll_answerC.setEnabled(true);
			ll_answerD.setEnabled(true);
		}else{
			ll_answerA.setEnabled(false);
			ll_answerB.setEnabled(false);
			ll_answerC.setEnabled(false);
			ll_answerD.setEnabled(false);
		}
//		if (isShowAnswer) {
//			tv_showAnswer.setText("答题模式");
//			if ("A".equals(curBean.answerOption)) {
//				imgv_optionA.setImageResource(R.drawable.radio_icon2);
//				imgv_optionB.setImageResource(R.drawable.radio_icon1);
//				imgv_optionC.setImageResource(R.drawable.radio_icon1);
//				imgv_optionD.setImageResource(R.drawable.radio_icon1);
//			} else if ("B".equals(curBean.answerOption)) {
//				imgv_optionA.setImageResource(R.drawable.radio_icon1);
//				imgv_optionB.setImageResource(R.drawable.radio_icon2);
//				imgv_optionC.setImageResource(R.drawable.radio_icon1);
//				imgv_optionD.setImageResource(R.drawable.radio_icon1);
//			} else if ("C".equals(curBean.answerOption)) {
//				imgv_optionA.setImageResource(R.drawable.radio_icon1);
//				imgv_optionB.setImageResource(R.drawable.radio_icon1);
//				imgv_optionC.setImageResource(R.drawable.radio_icon2);
//				imgv_optionD.setImageResource(R.drawable.radio_icon1);
//			} else if ("D".equals(curBean.answerOption)) {
//				imgv_optionA.setImageResource(R.drawable.radio_icon1);
//				imgv_optionB.setImageResource(R.drawable.radio_icon1);
//				imgv_optionC.setImageResource(R.drawable.radio_icon1);
//				imgv_optionD.setImageResource(R.drawable.radio_icon2);
//			}
			
//		} else {
//			tv_showAnswer.setText("查看答案");
//			if (TextUtils.isEmpty(rightAnswer)) {
//				imgv_optionA.setImageResource(R.drawable.radio_icon1);
//				imgv_optionB.setImageResource(R.drawable.radio_icon1);
//				imgv_optionC.setImageResource(R.drawable.radio_icon1);
//				imgv_optionD.setImageResource(R.drawable.radio_icon1);
//			} else {
//				if ("A".equals(rightAnswer)) {
//					imgv_optionA.setImageResource(R.drawable.radio_right);
//				}else if ("B".equals(rightAnswer)) {
//					imgv_optionB.setImageResource(R.drawable.radio_right);
//				}else if ("C".equals(rightAnswer)) {
//					imgv_optionC.setImageResource(R.drawable.radio_right);
//				}else if ("D".equals(rightAnswer)) {
//					imgv_optionD.setImageResource(R.drawable.radio_right);
//				}
//			}
//		}
//		if (TextUtils.isEmpty(rightAnswer)) {
//			imgv_optionA.setImageResource(R.drawable.radio_icon1);
//			imgv_optionB.setImageResource(R.drawable.radio_icon1);
//			imgv_optionC.setImageResource(R.drawable.radio_icon1);
//			imgv_optionD.setImageResource(R.drawable.radio_icon1);
//		} else {
//			if ("A".equals(rightAnswer)) {
//				imgv_optionA.setImageResource(R.drawable.radio_right);
//			}else if ("B".equals(rightAnswer)) {
//				imgv_optionB.setImageResource(R.drawable.radio_right);
//			}else if ("C".equals(rightAnswer)) {
//				imgv_optionC.setImageResource(R.drawable.radio_right);
//			}else if ("D".equals(rightAnswer)) {
//				imgv_optionD.setImageResource(R.drawable.radio_right);
//			}
//		}
		rightAnswer = "";
		tv_timu.setText(curBean.timu);
		tv_optionA.setText("A、"+curBean.optionA);
		tv_optionB.setText("B、"+curBean.optionB);
		if ("panduan".equals(curBean.timuType)) {
			 ll_answerC.setVisibility(View.GONE);
			 ll_answerD.setVisibility(View.GONE);
		} else {
			ll_answerC.setVisibility(View.VISIBLE);
			ll_answerD.setVisibility(View.VISIBLE);
			tv_optionC.setText("C、"+curBean.optionC);
			tv_optionD.setText("D、"+curBean.optionD);
		}
		if (curBean.bmp == null) {
			imgv_pic.setVisibility(View.GONE);
		} else {
			imgv_pic.setVisibility(View.VISIBLE);
			imgv_pic.setImageBitmap(curBean.bmp);
		}
	}
	
	private View.OnClickListener onClick() {
		return new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				switch (v.getId()) {
				case R.id.imageView_home:
					restoreTimuInfo();
					finish();
					break;
				case R.id.tv_pre:// 上一题
					if (curIndex == 0) {
						Toast.makeText(DatiActivity.this, "已经是第一题了！", Toast.LENGTH_SHORT).show();
						return;
					}
					curIndex--;
					resetViewData(isAnswerVisible,true,false,false);
					break;
				case R.id.tv_next:// 下一题
					if (curIndex == timuCount-1) {
						Toast.makeText(DatiActivity.this, "已经是最后一题了！", Toast.LENGTH_SHORT).show();
						return;
					}
					curIndex++;
//					if (curIndex==page*onePageSize-5) {// 请求下一页，应该放到动画开始或结束回调中
//						getTimuFromDB(getApplicationContext(), curTableName,timuType, isCuoti,++page);
//					} else {
						resetViewData(isAnswerVisible,true,false,false);
//					}
					break;
				case R.id.tv_showAnswer:// 显示答案
					isAnswerVisible = !isAnswerVisible;
					resetViewData(isAnswerVisible,true,false,false);
					break;
				case R.id.tv_remove2cuoti:// 标记错题，移至错题库
					if (curBean.isCuoti == 1) {// 原本已经标记过错题
						ll_timu.setBackgroundColor(Color.TRANSPARENT);
						tv_remove2Cuoti.setText("标记错题");
						curBean.isCuoti = 0;
					} else {
						ll_timu.setBackgroundColor(Color.RED);
						tv_remove2Cuoti.setText("移除错题");
						curBean.isCuoti = 1;
					}
					// 更新回数据库
					db.updateByIsCuoti(curTableName, curBean.id, curBean.isCuoti);
					break;
				case R.id.linearLayout_anwserA:// 选A
					if ("A".equals(curBean.answerOption)) {
						imgv_optionA.setImageResource(R.drawable.radio_right);
						curBean.duiCount++;
						db.updateByDuiCount(curTableName, curBean.id, curBean.duiCount);
						curIndex++;
//						if (curIndex==page*onePageSize-5) {// 请求下一页，应该放到动画开始或结束回调中
//							getTimuFromDB(getApplicationContext(), curTableName,timuType, isCuoti, ++page);
//						} else {
							resetViewData(isAnswerVisible,true,false,false);
//						}    
						rightAnswer = "";// 下一题答案设为空
					} else {// 选错
						imgv_optionA.setImageResource(R.drawable.radio_error);
						ll_timu.setBackgroundColor(Color.RED);
						tv_remove2Cuoti.setText("移除错题");
						curBean.isCuoti = 1;
						rightAnswer = curBean.answerOption; 
						if (isAnswerVisible) {
							isAnswerVisible = false;
							resetViewData(isAnswerVisible,false,true,false);
							isAnswerVisible = true;
						}else {
							resetViewData(isAnswerVisible,false,true,false);
						}
						curBean.cuoCount++;
						curBean.isCuoti = 1;
						db.updateByIsCuoti(curTableName, curBean.id, curBean.isCuoti);
						db.updateByCuoCount(curTableName, curBean.id, curBean.cuoCount);
					}
					break;
				case R.id.linearLayout_anwserB:// 选B
					if ("B".equals(curBean.answerOption)) {
						imgv_optionB.setImageResource(R.drawable.radio_right);
						curBean.duiCount++;
						db.updateByDuiCount(curTableName, curBean.id, curBean.duiCount);
						curIndex++;
//						if (curIndex==page*onePageSize-5) {// 请求下一页，应该放到动画开始或结束回调中
//							getTimuFromDB(getApplicationContext(), curTableName,timuType, isCuoti, ++page);
//						} else {
							resetViewData(isAnswerVisible,true,false,false);
//						}
						rightAnswer = "";// 下一题答案设为空
					} else {
						imgv_optionB.setImageResource(R.drawable.radio_error);
						ll_timu.setBackgroundColor(Color.RED);
						tv_remove2Cuoti.setText("移除错题");
						curBean.isCuoti = 1;
						rightAnswer = curBean.answerOption;
						if (isAnswerVisible) {
							isAnswerVisible = false;
							resetViewData(isAnswerVisible,false,true,false);
							isAnswerVisible = true;
						}else {
							resetViewData(isAnswerVisible,false,true,false);
						}
						curBean.cuoCount++;
						curBean.isCuoti = 1;
						db.updateByIsCuoti(curTableName, curBean.id, curBean.isCuoti);
						db.updateByCuoCount(curTableName, curBean.id, curBean.cuoCount);
					}
					break;
				case R.id.linearLayout_anwserC:// 选C
					if ("C".equals(curBean.answerOption)) {
						imgv_optionC.setImageResource(R.drawable.radio_right);
						curBean.duiCount++;
						db.updateByDuiCount(curTableName, curBean.id, curBean.duiCount);
						curIndex++;
//						if (curIndex==page*onePageSize-5) {// 请求下一页，应该放到动画开始或结束回调中
//							getTimuFromDB(getApplicationContext(), curTableName,timuType,  isCuoti,++page);
//						} else {
							resetViewData(isAnswerVisible,true,false,false);
//						}
						rightAnswer = "";// 下一题答案设为空
					} else {
						imgv_optionC.setImageResource(R.drawable.radio_error);
						ll_timu.setBackgroundColor(Color.RED);
						tv_remove2Cuoti.setText("移除错题");
						curBean.isCuoti = 1;
						rightAnswer = curBean.answerOption;
						if (isAnswerVisible) {
							isAnswerVisible = false;
							resetViewData(isAnswerVisible,false,true,false);
							isAnswerVisible = true;
						}else {
							resetViewData(isAnswerVisible,false,true,false);
						}
						curBean.cuoCount++;
						curBean.isCuoti = 1;
						db.updateByIsCuoti(curTableName, curBean.id, curBean.isCuoti);
						db.updateByCuoCount(curTableName, curBean.id, curBean.cuoCount);
					}
					break;
				case R.id.linearLayout_anwserD:// 选D
					if ("D".equals(curBean.answerOption)) {
						imgv_optionD.setImageResource(R.drawable.radio_right);
						curBean.duiCount++;
						db.updateByDuiCount(curTableName, curBean.id, curBean.duiCount);
						curIndex++;
//						if (curIndex==page*onePageSize-5) {// 请求下一页，应该放到动画开始或结束回调中
//							getTimuFromDB(getApplicationContext(), curTableName,timuType, isCuoti, ++page);
//						} else {
							resetViewData(isAnswerVisible,true,false,false);
//						}
						rightAnswer = "";// 下一题答案设为空 
					} else {
						imgv_optionD.setImageResource(R.drawable.radio_error);
						ll_timu.setBackgroundColor(Color.RED);
						tv_remove2Cuoti.setText("移除错题");
						curBean.isCuoti = 1;
						rightAnswer = curBean.answerOption;
						if (isAnswerVisible) {
							isAnswerVisible = false;
							resetViewData(isAnswerVisible,false,true,false);
							isAnswerVisible = true;
						}else {
							resetViewData(isAnswerVisible,false,true,false);
						}
						curBean.cuoCount++;
						curBean.isCuoti = 1;
						db.updateByIsCuoti(curTableName, curBean.id, curBean.isCuoti);
						db.updateByCuoCount(curTableName, curBean.id, curBean.cuoCount);
					}
					break;
				case R.id.tv_count:
					if (timuCount>1) {
						ll_alertContent = (LinearLayout)LayoutInflater.from(DatiActivity.this).inflate(R.layout.alert_content, null);
						editText = (EditText)ll_alertContent.findViewById(R.id.editText);
						new AlertDialog.Builder(DatiActivity.this)
							.setView(ll_alertContent)
							.setPositiveButton("确定", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog, int which) {
									String text = editText.getText().toString().trim();
									int tihao = 1;
									try {
										tihao = Integer.parseInt(text);
									} catch (NumberFormatException e) {
										Toast.makeText(DatiActivity.this, "请输入数字", Toast.LENGTH_SHORT).show();
										return;
									}
									if (tihao>timuCount||tihao<1) {
										Toast.makeText(DatiActivity.this, "不在范围内", Toast.LENGTH_SHORT).show();
										return;
									}
									getTimuByUserSelect(getApplicationContext(), curTableName, timuType, isCuoti, tihao);
								}
							})
							.setNegativeButton("取消", null).show();
					}
					break;
				}
			}
		};
	}
	
	private void getTimuFromDB(final Context ctx,final String tableName,
			final String timuType,final boolean isCuoti,final int page) {
		new Thread(){
			public void run() {
				UtilsManager.println("线程获取新的一页数据："+page);
				long start = System.currentTimeMillis();
				// 每次返回onePageSize条题目
//				Cursor cur = db.getOnePageTimuFromDB(tableName, timuType,isCuoti,(page-1)*onePageSize, onePageSize);
				Cursor cur = db.getOnePageTimuFromDBNew(tableName, timuType,isCuoti);
				if (cur!=null) {
					if (cur.moveToFirst()) {
						ShiTiBean bean;
						do {
							bean = new ShiTiBean();
							bean.localId = cur.getInt(0);
							bean.id = cur.getInt(1);
							if (isCuoti) {
								UtilsManager.println("往错题id数组加数据："+bean.id);
								timuIdsList.add(bean.id);
							}
							bean.jibie = cur.getString(2);
							bean.timuType = cur.getString(3);
							bean.timu = cur.getString(4);
							bean.optionA = cur.getString(5);
							if (bean.optionA.startsWith("A、")) {
								bean.optionA = bean.optionA.replaceFirst("A、", "");
							}
							bean.optionB = cur.getString(6);
							if (bean.optionB.startsWith("B、")) {
								bean.optionB = bean.optionB.replaceFirst("B、", "");
							}
							bean.optionC = cur.getString(7);
							if (bean.optionC.startsWith("C、")) {
								bean.optionC = bean.optionC.replaceFirst("C、", "");
							}
							bean.optionD = cur.getString(8);
							if (bean.optionD.startsWith("D、")) {
								bean.optionD = bean.optionD.replaceFirst("D、", "");
							}
							bean.answerOption = cur.getString(9);
							if (!TextUtils.isEmpty(bean.answerOption)) {
								bean.answerOption = bean.answerOption.trim();
								bean.answerOption = bean.answerOption.toUpperCase();
							}
							bean.answerDesc = cur.getString(10);
							bean.picUrl = cur.getString(11);
							if (!TextUtils.isEmpty(bean.picUrl)) {
								bean.picUrl = bean.picUrl.toLowerCase();
								bean.bmp = BitmapFactory.decodeFile(GloableData.DB_DIR+bean.picUrl);
							}
							bean.duiCount = cur.getInt(12);
							bean.cuoCount = cur.getInt(13);
							bean.isCuoti = cur.getInt(14);
							list.add(bean);
						} while (cur.moveToNext());
					}   
					cur.close();
				}
				UtilsManager.println("读数据耗时："+(System.currentTimeMillis()-start));
				handler.sendEmptyMessage(0);//设置视图数据
			}
		}.start();
	}
	
	private void getTimuFromDBByLastRead(final Context ctx,final String tableName,
			final String timuType,final boolean isCuoti,final int localId) {
		new Thread(){
			public void run() {
				UtilsManager.println("线程获取新的一页数据："+page);
				// 每次返回onePageSize条题目
//				Cursor cur = db.getOnePageTimuFromDBByLastRead(tableName, timuType,isCuoti,localId+onePageSize);
				Cursor cur = db.getOnePageTimuFromDBByLastReadNew(tableName, timuType,isCuoti);
				if (cur!=null) {
					if (cur.moveToFirst()) {
						ShiTiBean bean;
						do {
							bean = new ShiTiBean();
							bean.localId = cur.getInt(0);
							bean.id = cur.getInt(1);
							if (isCuoti) {
								UtilsManager.println("往错题id数组加数据："+bean.id);
								timuIdsList.add(bean.id);
							}
							bean.jibie = cur.getString(2);
							bean.timuType = cur.getString(3);
							bean.timu = cur.getString(4);
							bean.optionA = cur.getString(5);
							if (bean.optionA.startsWith("A、")) {
								bean.optionA = bean.optionA.replaceFirst("A、", "");
							}
							bean.optionB = cur.getString(6);
							if (bean.optionB.startsWith("B、")) {
								bean.optionB = bean.optionB.replaceFirst("B、", "");
							}
							bean.optionC = cur.getString(7);
							if (bean.optionC.startsWith("C、")) {
								bean.optionC = bean.optionC.replaceFirst("C、", "");
							}
							bean.optionD = cur.getString(8);
							if (bean.optionD.startsWith("D、")) {
								bean.optionD = bean.optionD.replaceFirst("D、", "");
							}
							bean.answerOption = cur.getString(9);
							if (!TextUtils.isEmpty(bean.answerOption)) {
								bean.answerOption = bean.answerOption.trim();
								bean.answerOption = bean.answerOption.toUpperCase();
							}
							bean.answerDesc = cur.getString(10);
							bean.picUrl = cur.getString(11);
							if (!TextUtils.isEmpty(bean.picUrl)) {
								bean.picUrl = bean.picUrl.toLowerCase();
								bean.bmp = BitmapFactory.decodeFile(GloableData.DB_DIR+bean.picUrl);
							}
							bean.duiCount = cur.getInt(12);
							bean.cuoCount = cur.getInt(13);
							bean.isCuoti = cur.getInt(14);
							list.add(bean);
						} while (cur.moveToNext());
					}
					cur.close();
				}
//				if ("xuanze".equals(timuType)) {
//					int count = db.getPanduanCount(curTableName);
//					UtilsManager.println("count:"+count);
//					UtilsManager.println("localId:"+localId);
//					if (count>0) {
//						curIndex = localId-count;
//					} else {
//						curIndex = localId-1;
//					}
//				} else {
				UtilsManager.println("localId:"+localId);
					if (!isCuoti) {
						curIndex = localId-1;
					} else {// 重新找当前题目索引
						int size = timuIdsList.size();
						for (int i = 0; i < size; i++) {
							if (timuIdsList.get(i) == localId) {
								curIndex = i;
								break;
							}
						}
					}
					UtilsManager.println("curIndex:"+curIndex);
//				}
				handler.sendEmptyMessage(0);//设置视图数据
			}
		}.start();
	}
	
	private void getTimuByUserSelect(final Context ctx,final String tableName,
			final String timuType,final boolean isCuoti,final int tihao) {
		new Thread(){
			public void run() {
				UtilsManager.println("开始定位："+tihao);
				// 每次返回onePageSize条题目
//				Cursor cur = db.getOnePageTimuFromDBByLastRead(tableName, timuType,isCuoti,tihao+onePageSize);
				Cursor cur = db.getOnePageTimuFromDBByLastReadNew(tableName, timuType,isCuoti);
				if (cur!=null) {
					if (cur.moveToFirst()) {
						ShiTiBean bean;
						list.clear();
						do {
							bean = new ShiTiBean();
							bean.localId = cur.getInt(0);
							bean.id = cur.getInt(1);
							bean.jibie = cur.getString(2);
							bean.timuType = cur.getString(3);
							bean.timu = cur.getString(4);
							bean.optionA = cur.getString(5);
							if (bean.optionA.startsWith("A、")) {
								bean.optionA = bean.optionA.replaceFirst("A、", "");
							}
							bean.optionB = cur.getString(6);
							if (bean.optionB.startsWith("B、")) {
								bean.optionB = bean.optionB.replaceFirst("B、", "");
							}
							bean.optionC = cur.getString(7);
							if (bean.optionC.startsWith("C、")) {
								bean.optionC = bean.optionC.replaceFirst("C、", "");
							}
							bean.optionD = cur.getString(8);
							if (bean.optionD.startsWith("D、")) {
								bean.optionD = bean.optionD.replaceFirst("D、", "");
							}
							bean.answerOption = cur.getString(9);
							if (!TextUtils.isEmpty(bean.answerOption)) {
								bean.answerOption = bean.answerOption.trim();
								bean.answerOption = bean.answerOption.toUpperCase();
							}
							bean.answerDesc = cur.getString(10);
							bean.picUrl = cur.getString(11);
							if (!TextUtils.isEmpty(bean.picUrl)) {
								bean.picUrl = bean.picUrl.toLowerCase();
								bean.bmp = BitmapFactory.decodeFile(GloableData.DB_DIR+bean.picUrl);
							}
							bean.duiCount = cur.getInt(12);
							bean.cuoCount = cur.getInt(13);
							bean.isCuoti = cur.getInt(14);
							list.add(bean);
						} while (cur.moveToNext());
					}
					cur.close();
				}
				curIndex = tihao-1;
				handler.sendEmptyMessage(0);//设置视图数据
			}
		}.start();
	}
	
	private int getTimuCountFromDB(String tableName,String timuType,boolean isCuoti) {
		return db.getCountFromDB(tableName,timuType,isCuoti);
	}
	
	
	private void restoreTimuInfo() {
		if (curBean==null){
			return;
		}
		LastReadInfoBean bean = new LastReadInfoBean();
		bean.typeId = UtilsManager.getTypeId(whichTable, timuType, isCuoti);
		//  仅适用于非错题的情况下
		if (isCuoti) {
			if (timuIdsList.size()>=curIndex) {
				bean.localId = timuIdsList.get(curIndex);
				if (bean.localId == timuIdsList.get(0)) {
					UtilsManager.println("这是第一题，删掉原记录,localId:"+bean.localId);
					db.deleteOneCuotiLastReadInfo(bean.typeId);
					return;
				}
				db.insert2CuotiLastReadInfo(bean);
			}
		} else {
			if ("xuanze".equals(timuType)) {
				int count = db.getPanduanCount(curTableName);
				if (count<0) {
					return;
				}
				bean.localId = curBean.localId-count;
			} else {
				bean.localId = curBean.localId;
			}
			if (bean.localId<=1){
				UtilsManager.println("这是第一题，删掉原记录,localId:"+bean.localId);
				db.deleteOneLastReadInfo(bean.typeId);
				return;
			}
			db.insert2LastReadInfo(bean);
		}
	}
	
	@Override
	public void onBackPressed() {
		restoreTimuInfo();
		super.onBackPressed();
	}

	@Override
	protected void onStop() {
		restoreTimuInfo();
		super.onStop();
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		restoreTimuInfo();
		outState.putInt("receiveLocalId", curIndex+1);
		outState.putInt("whichTable", whichTable);
		outState.putBoolean("isCuoti", isCuoti);
		outState.putString("timuType", timuType);
		UtilsManager.println("这里保存activity状态了！");
	}



	Handler handler = new Handler(){
		@Override
		public void handleMessage(Message msg) {
			if (DatiActivity.this.isFinishing()) {
				return;
			}
			switch (msg.what) {
			case 0:
				resetViewData(isAnswerVisible,true,false,false);
				break;
			default:
				break;
			}
			super.handleMessage(msg);
		}
		
	};
}
